package com.dunqi.gpm.cangxi.home.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Paint;
import android.support.annotation.ColorRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dunqi.gpm.cangxi.R;
import com.dunqi.gpm.cangxi.common.util.CommonUtil;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by 空 on 2017/11/22 0022.
 */

public class NormalView extends FrameLayout {

    @BindView(R.id.des_tv)
    TextView desTv;
    @BindView(R.id.content_tv)
    TextView contentTv;

    StringBuilder sb;

    public NormalView(@NonNull Context context) {
        this(context, null);
    }

    public NormalView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public NormalView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(@NonNull Context context, @Nullable AttributeSet attrs) {
        LayoutInflater.from(context).inflate(R.layout.normal_view, this);
        ButterKnife.bind(this);
        TypedArray array = getContext().obtainStyledAttributes(attrs, R.styleable.normal);
        int anInt = array.getInt(R.styleable.normal_max_word, 5);
        String des = array.getString(R.styleable.normal_des);
        int color = array.getColor(R.styleable.normal_text_color, 0xFFFFFFFD);

        if (0xFFFFFFFD != color) {
            contentTv.setTextColor(color);
        }
        setdesLength(anInt);
        desTv.setText(des);
        array.recycle();
    }

    public void setdesLength(int anInt) {
        sb = new StringBuilder();
        for (int i = 0; i < anInt; i++) {
            sb.append("哈");
        }
        String s = sb.toString();
        ViewGroup.LayoutParams params = desTv.getLayoutParams();
        Paint paint = new Paint();
        paint.setTextSize(CommonUtil.dip2px(14));
        float measureText = paint.measureText(s);
        params.width = (int) measureText;
    }

    public void setText(String text) {
        contentTv.setText(text);
    }

    public void setTextColor(@ColorRes int color) {
        contentTv.setTextColor(CommonUtil.getColor(color));
    }

    public void setDesColor(@ColorRes int color) {
        desTv.setTextColor(CommonUtil.getColor(color));
    }


    public void setDes(String s) {
        desTv.setText(s);
    }

    public TextView getDesTv() {
        return desTv;
    }

    public void setDesTv(TextView desTv) {
        this.desTv = desTv;
    }

    public TextView getContentTv() {
        return contentTv;
    }

    public void setContentTv(TextView contentTv) {
        this.contentTv = contentTv;
    }

    public void setDesWidthMatchContent() {
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
        lp.gravity = Gravity.END;
        desTv.setLayoutParams(lp);
    }

    public void setAllViewTextSize(float textSize) {
        desTv.setTextSize(textSize);
        contentTv.setTextSize(textSize);
    }

    public void setLeftViewAsMasterStyle() {
        setDesColor(R.color.black);
        setTextColor(R.color.text_color_select_gray);
    }

    public void setRightViewAsMasterStyle() {
        setDesColor(R.color.text_color_select_gray);
        setTextColor(R.color.black);
    }

    public void setAllViewAsMasterStyle() {
        setDesColor(R.color.black);
        setTextColor(R.color.black);
    }

    public void setAllViewAsSecondStyle() {
        setDesColor(R.color.text_color_select_gray);
        setTextColor(R.color.text_color_select_gray);
    }
}
